//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.yaukie.base.util;

import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;
import org.yaukie.base.text.Convert;

public class ServletUtils {
    public ServletUtils() {
    }

    public static String renderString(HttpServletResponse response, String string) {
        try {
            response.setStatus(200);
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Headers", "Authorization, authorization,token, Accept, Origin, X-Requested-With, Content-Type, Last-Modified");
            response.setHeader("Content-type", "application/json;charset=UTF-8");
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().print(string);
        } catch (IOException var3) {
            var3.printStackTrace();
        }

        return null;
    }

    public static boolean isAjaxRequest(HttpServletRequest request) {
        String accept = request.getHeader("accept");
        if (accept != null && accept.indexOf("application/json") != -1) {
            return true;
        } else {
            String xRequestedWith = request.getHeader("X-Requested-With");
            if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1) {
                return true;
            } else {
                String uri = request.getRequestURI();
                if (StringTools.inStringIgnoreCase(uri, new String[]{".json", ".xml"})) {
                    return true;
                } else {
                    String ajax = request.getParameter("__ajax");
                    return StringTools.inStringIgnoreCase(ajax, new String[]{"json", "xml"});
                }
            }
        }
    }

    public static String getParameter(HttpServletRequest request, String name) {
        return request.getParameter(name);
    }

    public static String getParameter(HttpServletRequest request, String name, String defaultValue) {
        return Convert.toStr(request.getParameter(name), defaultValue);
    }

    public static Integer getParameterToInt(HttpServletRequest request, String name) {
        return Convert.toInt(request.getParameter(name));
    }

    public static Integer getParameterToInt(HttpServletRequest request, String name, Integer defaultValue) {
        return Convert.toInt(request.getParameter(name), defaultValue);
    }

    public static String getBody(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        InputStream inputStream = null;
        BufferedReader reader = null;
        String line = null;

        try {
            inputStream = request.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

            while(reader.readLine() != null) {
                sb.append(reader.readLine());
            }
        } catch (IOException var18) {
            var18.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException var17) {
                    var17.printStackTrace();
                }
            }

            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException var16) {
                    var16.printStackTrace();
                }
            }

        }

        return sb.toString();
    }

    public static Map<String, String> getParamMap(HttpServletRequest request) {
        Map<String, String[]> paramMap = request.getParameterMap();
        Map<String, String> rtnMap = new HashMap();
        Iterator var3 = paramMap.keySet().iterator();

        while(var3.hasNext()) {
            String key = (String)var3.next();
            rtnMap.put(key, ((String[])paramMap.get(key))[0]);
        }

        return rtnMap;
    }

    public static String argsArrayToString(Object[] paramsArray) {
        String params = "";
        if (paramsArray != null && paramsArray.length > 0) {
            for(int i = 0; i < paramsArray.length; ++i) {
                if (!isFilterObject(paramsArray[i])) {
                    Object jsonObj = JSON.toJSON(paramsArray[i]);
                    params = params + jsonObj.toString() + " ";
                }
            }
        }

        return params.trim();
    }

    public static boolean isFilterObject(final Object o) {
        Class<?> clazz = o.getClass();
        if (clazz.isArray()) {
            return clazz.getComponentType().isAssignableFrom(MultipartFile.class);
        } else {
            Iterator iter;
            if (Collection.class.isAssignableFrom(clazz)) {
                Collection collection = (Collection)o;
                iter = collection.iterator();
                if (iter.hasNext()) {
                    return iter.next() instanceof MultipartFile;
                }
            } else if (Map.class.isAssignableFrom(clazz)) {
                Map map = (Map)o;
                iter = map.entrySet().iterator();
                if (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry)iter.next();
                    return entry.getValue() instanceof MultipartFile;
                }
            }

            return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse || o instanceof BindingResult;
        }
    }
}
